<?php

namespace app\api\model\wanlshop;

use think\Db;
use think\Model;

class ActCashBack extends Model
{
    // 表名
    protected $name = 'act_cash_back';

    /**
     *  提现操作
     */

    public function insData($ins)
    {
        $this->insert($ins);
    }

    /**
     * 活动提现列表
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function getMyWithdrawalCashLists($uid, $page, $pageSize)
    {
        // 条件
        $where["user_id"] = $uid;
        $where["w_type"] = 1;
//        $where["user_id"] = 3;
        // 总条数
        $count = $this->where($where)->count();
        // 数据
        $list = $this->field('id, user_id, FROM_UNIXTIME(act_available_time) as act_available_time, w_act_state, FROM_UNIXTIME(createtime) as createtime, amount_withdrawal')
            ->where($where)
            ->order("id desc")
            ->paginate($pageSize, $count, ['page' => $page])
            ->toArray();
        $all_price_arr = 0;
        foreach ($list["data"] as $key => $item) {
            if ($item['w_act_state'] == 1 && $item['act_available_time'] > date('Y-m-d H:i:s')) {
                $all_price_arr += $item["amount_withdrawal"];
            }
            if ($item['act_available_time'] < date('Y-m-d H:i:s') && $item['w_act_state'] == 1) {
                ActCashBack::where('id', $item['id'])->update(['w_act_state' => 3]);
                $list["data"][$key]['w_act_state'] = 3;
            }
            $list["data"][$key]['amount'] = $item['amount_withdrawal'];
        }
        $list['all_price'] = $all_price_arr;
        return $list;
    }

}
